package scriptmanager

import (
	"MqttCloudProtocolServer/ProtocolServer/scriptmanager/modulelib"
	"errors"
	"path"
	"strings"

	"github.com/wonderivan/logger"
)

//GetScriptManager 根据脚本后缀决定启用脚本管理器
func GetScriptManager(scriptName string, param string) IScriptManager {
	//manager := new(LuaManager)
	var manager IScriptManager
	scriptExtName := strings.ToUpper(path.Ext(scriptName))
	switch scriptExtName {
	case ".LUA":
		manager = new(LuaManager)
	case ".PY":
		manager = nil
	case ".JS":
		manager = new(JavaScriptManager)
	case ".DLL":
		manager = nil
	default:
		manager = nil
	}
	if manager != nil {
		if res, err := manager.Init(scriptName, param); !res {
			logger.Error(err)
			return nil
		}
	}
	return manager
}

//IScriptManager ScriptManager接口，供系统程序调用，从父类控制
type IScriptManager interface {
	Init(scriptName string, param string) (bool, error)
	Start() (bool, error)
	MQTTReciveToScript(buffer []uint8) (bool, error)
	SetMQTTSender(func(buffer []uint8) (bool, error))
	Stop() (bool, error)
	GetKey() string
	SetKey(key string)
}

//ScriptBase 脚本管理器。作为基类，所有需要继承的函数接口用事件表示。同时实现IScriptManager中发送函数的回调
type ScriptBase struct {
	key        string
	tcpServer  *modulelib.TCPServer
	timeTicker *modulelib.TimeTicker
	mqttSender func(buffer []uint8) (bool, error)
}

//GetKey Fun
func (_this *ScriptBase) GetKey() string {
	return _this.key
}

//SetKey Fun
func (_this *ScriptBase) SetKey(key string) {
	_this.key = key
}

//---------------------MQTT部分--------------------------

//SetMQTTSender 设置MQTT发送器
func (_this *ScriptBase) SetMQTTSender(mqttSender func(buffer []uint8) (bool, error)) {
	_this.mqttSender = mqttSender
}

//MQTTSendFromScript 用于脚本调用发送MQTT数据
func (_this *ScriptBase) MQTTSendFromScript(buffer []uint8) (bool, error) {
	if _this.mqttSender != nil {
		return _this.mqttSender(buffer)
	}
	return false, errors.New("MQTT发送器没有配置")
}

//---------------------MQTT部分--------------------------

//---------------------TCP部分--------------------------

func (_this *ScriptBase) getTCPServer() *modulelib.TCPServer {
	if _this.tcpServer == nil {
		_this.tcpServer = new(modulelib.TCPServer)
	}
	return _this.tcpServer
}

//TCPInit 初始化TCP链接
func (_this *ScriptBase) TCPInit(ip string, port int) bool {
	return _this.getTCPServer().Init(ip, port, false)
}

//TCPSetReceivEvent 设置接收回调函数
func (_this *ScriptBase) TCPSetReceivEvent(receiver func(key string, buffer []uint8)) {
	_this.getTCPServer().SetReceivEvent(receiver)
}

//TCPSetAcceptEvent 设置连接成功时事件函数
func (_this *ScriptBase) TCPSetAcceptEvent(acceptEvent func(key string)) {
	_this.getTCPServer().SetAcceptEvent(acceptEvent)
}

//TCPSend TCP发送数据
func (_this *ScriptBase) TCPSend(key string, buffer []uint8) (int, error) {
	if _this.tcpServer != nil {
		return _this.tcpServer.Send(key, buffer)
	}
	return 0, errors.New("TCP服务未初始化")
}

//TCPClose 关闭TCP链接
func (_this *ScriptBase) TCPClose() {
	if _this.tcpServer != nil {
		if err := _this.tcpServer.Close(); err != nil {
			logger.Error(err)
		}
	}
}

//---------------------TCP部分--------------------------

//---------------------实时库部分--------------------------

//RTDBWrite 实时库写入数据
func (_this *ScriptBase) RTDBWrite() {}

//RTDBRead 实时库读取数据
func (_this *ScriptBase) RTDBRead() {}

//---------------------实时库部分--------------------------

//---------------------历史库部分--------------------------

//HisDBWrite 历史库写入数据
func (_this *ScriptBase) HisDBWrite() {}

//HisDBRead 历史库读取数据
func (_this *ScriptBase) HisDBRead() {}

//---------------------历史库部分--------------------------

//---------------------定时器部分--------------------------

func (_this *ScriptBase) getTimer() *modulelib.TimeTicker {
	if _this.timeTicker == nil {
		_this.timeTicker = new(modulelib.TimeTicker)
	}
	return _this.timeTicker
}

//TimerInit 初始化定时器
func (_this *ScriptBase) TimerInit(durMillisecond int64) {
	_this.getTimer().Init(durMillisecond)
}

//TimerSetElapsed 设置接收回调函数
func (_this *ScriptBase) TimerSetElapsed(elapsed func()) {
	_this.getTimer().SetElapsed(elapsed)
}

//TimerStart 启动定时器
func (_this *ScriptBase) TimerStart() {
	if _this.timeTicker != nil {
		_this.timeTicker.Start()
	}
}

//TimerClose 关闭定时器
func (_this *ScriptBase) TimerClose() {
	if _this.timeTicker != nil {
		_this.timeTicker.Close()
	}
}

//TimerStop 关闭定时器
func (_this *ScriptBase) TimerStop() {
	if _this.timeTicker != nil {
		_this.timeTicker.Stop()
	}
}

//---------------------定时器部分--------------------------
